
close all;
clc;

%% Trade-off between inductor and capacitor

cc=hsv(3);
fsw=100e3;
C=logspace(-7,-3);
L=(1/(fsw*2*pi))^2./C;
Pout=1;
R=[0.1 1 10];
V=sqrt(Pout.*R);
I=sqrt(Pout./R);
kc=1+2^2+3^2+4^2+5^2+6^2+7^2;     % 8 to 1 dickson
kvc=1;  % volume per energy storage of capacitor
kvl=kvc*1000;   % volume per energy storage of inductor
figure;     % cosntant fsw, change R

h=[]
for i=1:length(R)  
% for i=2
    VolumeC=kvc*kc*0.5.*C*V(i)^2;
    VolumeL=kvl*0.5.*L*I(i)^2;
    Volumetot=VolumeC+VolumeL;
    ratio=C./L;
    Ron=R(i)*10/100;  % Ron of switches =5% of the load, -> 95% conduction efficiency
    C_hard=1/(2*pi*fsw*Ron);
    VolumeC_hard=kvc*kc*0.5.*C_hard*V(i)^2;
    
    
    h1=loglog(C./L,Volumetot/VolumeC_hard,'-','color',cc(i,:),'LineWidth',1.5)
    h=[h h1];
    hold on;
%     plot(C./L,VolumeC,'--','color',cc(i,:),'LineWidth',1)

    %% plot the vertical lines representing the improvement over hard-charging
%     Q=Ron/2*sqrt(C./L);
%     ratio_under_damped_1=(1/Ron/1)^2;
%     ratio_under_damped_10=(1/Ron/10)^2;
%     ratio_under_damped_100=(1/Ron/100)^2;
%     plot([ratio_under_damped_1 ratio_under_damped_1], [10^-4 1],'--','color',cc(i,:),'LineWidth',1.5)
%     plot([ratio_under_damped_10 ratio_under_damped_10], [10^-4 1],'--','color',cc(i,:),'LineWidth',1.5)
%     plot([ratio_under_damped_100 ratio_under_damped_100], [10^-4 1],'--','color',cc(i,:),'LineWidth',1.5)

    %% Plot the total capacitor volume of hard-charging converters
    % assuming same corner frequency, i.e. simililar efficiency
%     plot([10^-3, 10^5] , [VolumeC_hard VolumeC_hard],'--','color',cc(i,:),'LineWidth',1.5)
end
% for i=1:length(R)  
% % for i=2
%     VolumeC=kvc*kc*0.5.*C*V(i)^2;
%     VolumeL=kvl*0.5.*L*I(i)^2;
%     Volumetot=VolumeC+VolumeL;
%     ratio=C./L;
%     Ron=R(i)*1/100;  % Ron of switches =5% of the load, -> 95% conduction efficiency
%     C_hard=1/(2*pi*fsw*Ron);
%     VolumeC_hard=kvc*kc*0.5.*C_hard*V(i)^2;
%     
%     
%     h1=loglog(C./L,Volumetot/VolumeC_hard,'-','color',cc(i,:),'LineWidth',1.5)
%     h=[h h1];
%     hold on;
% %     plot(C./L,VolumeC,'--','color',cc(i,:),'LineWidth',1)
% 
%     %% plot the vertical lines representing the improvement over hard-charging
% %     Q=Ron/2*sqrt(C./L);
% %     ratio_under_damped_1=(1/Ron/1)^2;
% %     ratio_under_damped_10=(1/Ron/10)^2;
% %     ratio_under_damped_100=(1/Ron/100)^2;
% %     plot([ratio_under_damped_1 ratio_under_damped_1], [10^-4 1],'--','color',cc(i,:),'LineWidth',1.5)
% %     plot([ratio_under_damped_10 ratio_under_damped_10], [10^-4 1],'--','color',cc(i,:),'LineWidth',1.5)
% %     plot([ratio_under_damped_100 ratio_under_damped_100], [10^-4 1],'--','color',cc(i,:),'LineWidth',1.5)
% 
%     %% Plot the total capacitor volume of hard-charging converters
%     % assuming same corner frequency, i.e. simililar efficiency
% %     plot([10^-3, 10^5] , [VolumeC_hard VolumeC_hard],'--','color',cc(i,:),'LineWidth',1.5)
% end
grid on;

xlabel('Capacitance over inductance [F/H]')
ylabel('Normalized volume')
xlim([5^-5,20^5]);
ylim([10^-1,10^2])

set(gcf,'PaperPositionMode','auto')
width=250;
height=width*0.2;
set(gcf, 'Position', [200 200 200+width 200+height])
% set(gca,'FontSize',12,'fontWeight','normal','LineWidth',1.25)
% set(findall(gcf,'type','text'),'FontSize',12,'fontWeight','normal')

set(gca,'xcolor',[0.75 0.75 0.75],'ycolor',[0.75 0.75 0.75])
set(gca,'MinorGridLineStyle','-')
grid on
c=copyobj(gca,gcf);
set(c,'color','none','xcolor','k', ...
'xgrid','off','ycolor','k', ...
'ygrid','off');
legend(h,'R_{load}=0.1 \Omega','R_{load}=1 \Omega','R_{load}=10 \Omega')
print('-depsc2', './cap_ind');
% break
%% hard vs soft without regulation
fsw=100e3;
C=logspace(-7,-3);
L=(1/(fsw*2*pi))^2./C;
Pout=1;
Ron=logspace(-2,0);
Rload=[1];
VolumeHard=Ron;
% Ron=[0.01 0.01 0.01];
V=sqrt(Pout.*Rload);
I=sqrt(Pout./Rload);
kc=1+2^2+3^2+4^2+5^2+6^2+7^2;     % 8 to 1 dickson

kr=1;
figure;     % cosntant fsw, change R
cc=hsv(length(Ron));
for i=1:length(Ron)   
    VolumeC=kc*0.5.*C*V(1)^2;
    VolumeL=0.5.*L*I(1)^2*1000;
    Volumetot=VolumeC+VolumeL;
    [VolumeMin,index]=min(Volumetot);
    Copt=C(index);
    Chard=1/(2*pi*fsw)/Ron(i)/kr;
    VolumeHard(i)=kc*0.5*Chard*V(1)^2;
%     loglog(C,VolumeC,'--','color',cc(i,:),'LineWidth',1)
%     loglog(C,VolumeL,'-.','color',cc(i,:),'LineWidth',1)
end
loglog(Ron,VolumeMin./VolumeHard,'--r','LineWidth',1.5)
hold on;
% loglog([1e-2 1e0],[VolumeMin VolumeMin],'-b','LineWidth',1.5)
loglog([1e-2 1e0], [1 1],'-b','LineWidth',1.5)


xlabel('R_{ds,on}/R_{load}')
ylabel('Normalized Volume')

set(gcf,'PaperPositionMode','auto')
width=250;
height=width*0.2;
set(gcf, 'Position', [200 200 200+width 200+height])
% set(gca,'FontSize',12,'fontWeight','normal','LineWidth',1.25)
% set(findall(gcf,'type','text'),'FontSize',12,'fontWeight','normal')

set(gca,'xcolor',[0.75 0.75 0.75],'ycolor',[0.75 0.75 0.75])
set(gca,'MinorGridLineStyle','-')
grid on
c=copyobj(gca,gcf);
set(c,'color','none','xcolor','k', ...
'xgrid','off','ycolor','k', ...
'ygrid','off');
legend('Soft-charging')
